package com.five.monkey.feign.es.service.impl;

import com.five.monkey.feign.es.convert.GraphConvert;
import com.five.monkey.feign.es.dao.model.Graph;
import com.five.monkey.feign.es.dao.repository.GraphRepository;
import com.five.monkey.feign.es.service.GraphElasticsearchService;
import com.five.monkey.vo.GraphVo;
import com.five.monkey.vo.PageVo;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
//import org.springframework.data.elasticsearch.core.SearchHitSupport;
//import org.springframework.data.elasticsearch.core.SearchHits;
//import org.springframework.data.elasticsearch.core.SearchPage;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author 狻猊
 * @date 2021-11-25 14:40:05
 */
@Service
public class GraphElasticsearchServiceImpl implements GraphElasticsearchService {

    @Autowired
    private ElasticsearchOperations elasticsearchOperations;

    @Autowired
    private GraphRepository graphRepository;

    @Override
    public GraphVo save(GraphVo graphVo) {
        Graph graph = GraphConvert.vo2esModel(graphVo);
        if (Objects.nonNull(graph)) {
            graph = graphRepository.save(graph);
        }
        return GraphConvert.esModel2vo(graph);
    }

    @Override
    public GraphVo findById(Long id) {
        Graph graph = graphRepository.findById(id).orElse(null);
        return GraphConvert.esModel2vo(graph);
    }

    @Override
    public List<GraphVo> findAll() {
        List<GraphVo> graphVoList = Lists.newArrayList();
        graphRepository.findAll(Sort.by(Sort.Order.asc("id")))
                .forEach(graph -> graphVoList.add(GraphConvert.esModel2vo(graph)));
        return graphVoList;
    }

    @Override
    public boolean deleteById(Long id) {
        graphRepository.deleteById(id);
        return true;
    }

    @Override
    public List<GraphVo> findByStatus(Integer status) {
        Criteria criteria = Criteria.where("status").is(status);
        Query query = new CriteriaQuery(criteria);
        query.addSort(Sort.by(Sort.Order.asc("id")));
        //List<Graph> graphList = elasticsearchOperations.multiGet(query, Graph.class);
        List<Graph> graphList = Lists.newArrayList();
        return graphList.stream().map(GraphConvert::esModel2vo).collect(Collectors.toList());
    }

    @Override
    public PageVo<GraphVo> pageByStatus(Integer status, int page, int size, Sort sort) {
        if (Objects.isNull(sort)) {
            sort = Sort.by(Sort.Order.asc("id"));
        }
        Pageable pageable = PageRequest.of(page, size, sort);
        Criteria criteria = Criteria.where("status").is(status);
        Query query = new CriteriaQuery(criteria);
        query.setPageable(pageable);
        //SearchHits<Graph> graphSearchHits = elasticsearchOperations.search(query, Graph.class);
        //SearchPage<Graph> graphSearchPage = SearchHitSupport.searchPageFor(graphSearchHits, pageable);
        //return new PageConvert<Graph, GraphVo>().searchPage2vo(graphSearchPage, GraphConvert::esModel2vo);
        return null;
    }
}
